package com.ruoyi.develop.produceRecord.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.produceRecordDetail.domain.ProduceRecordDetail;
import com.ruoyi.develop.produceRecordDetail.service.IProduceRecordDetailService;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.produceRecord.mapper.ProduceRecordMapper;
import com.ruoyi.develop.produceRecord.domain.ProduceRecord;
import com.ruoyi.develop.produceRecord.service.IProduceRecordService;

/**
 * 生产记录Service业务层处理
 *
 * @author lmm
 * @date 2025-05-16
 */
@Service
public class ProduceRecordServiceImpl implements IProduceRecordService {
    @Resource
    private ProduceRecordMapper produceRecordMapper;

    @Resource
    private IProduceRecordDetailService produceRecordDetailService;


    /**
     * 查询生产记录
     *
     * @param id 生产记录主键
     * @return 生产记录
     */
    @Override
    public ProduceRecord selectById(String id) {
        return produceRecordMapper.selectById(id);
    }

    /**
     * 查询生产记录列表
     *
     * @param entity 生产记录
     * @return 生产记录
     */
    @Override
    public List<ProduceRecord> selectList(ProduceRecord entity) {
        return produceRecordMapper.selectList(entity);
    }

    /**
     * 新增生产记录
     *
     * @param entity 生产记录
     * @return 结果
     */
    @Override
    public int insert(ProduceRecord entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isBlank(entity.getNos())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String today = sdf.format(entity.getSubmitDate()==null?new Date():entity.getSubmitDate());
            String codeMaxString = this.selectMaxCode(today);
            if(StringUtils.isNotBlank(codeMaxString)){
                // 最大编号+1
                codeMaxString = Long.parseLong(codeMaxString)+1+"";
            }else{
                codeMaxString = today+"0001";
            }
            entity.setNos(codeMaxString);
        }
        List<ProduceRecordDetail> details = entity.getDetailList();
        for (ProduceRecordDetail detail : details) {
            detail.setProduceId(entity.getId());
            detail.setProduceNos(entity.getNos());
            detail.setStatus(BusinessConstants.PRODUCE_RECORD_STATUS_UN);
            if (StringUtils.isBlank(detail.getGoodsId())){
                continue;
            }
            produceRecordDetailService.insert(detail);
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return produceRecordMapper.insert(entity);
    }

    /** * 获取最大的编号  */
    @Override
    public String selectMaxCode(String date) {
        return produceRecordMapper.selectMaxCode(date);
    }

    /**
     * 修改生产记录
     *
     * @param entity 生产记录
     * @return 结果
     */
    @Override
    public int update(ProduceRecord entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());

        List<ProduceRecordDetail> details = entity.getDetailList();
        if (details != null && details.size() > 0){
            for (ProduceRecordDetail detail : details) {
                if(StringUtils.isBlank(detail.getId())){
                    detail.setProduceId(entity.getId());
                    detail.setProduceNos(entity.getNos());
                    if (StringUtils.isBlank(detail.getGoodsId())){
                        continue;
                    }
                    produceRecordDetailService.insert(detail);
                }
                if(StringUtils.isNotBlank(detail.getId())){
                    produceRecordDetailService.update(detail);
                }
            }
        }
        return produceRecordMapper.update(entity);
    }

    /**
     * 批量删除生产记录
     *
     * @param ids 需要删除的生产记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        for (String id : ids) {
            ProduceRecordDetail params = new ProduceRecordDetail();
            params.setProduceId(id);
            List<ProduceRecordDetail> details = produceRecordDetailService.selectList(params);
            for (ProduceRecordDetail detail : details) {
                produceRecordDetailService.deleteById(detail.getId());
            }
        }
        return produceRecordMapper.deleteByIds(ids);
    }

    /**
     * 删除生产记录信息
     *
     * @param id 生产记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        ProduceRecordDetail params = new ProduceRecordDetail();
        params.setProduceId(id);
        List<ProduceRecordDetail> details = produceRecordDetailService.selectList(params);
        for (ProduceRecordDetail detail : details) {
            produceRecordDetailService.deleteById(detail.getId());
        }
        return produceRecordMapper.deleteById(id);
    }
}
